package com.example.myapplicationktkt2.activity

import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.example.myapplicationktkt2.R
import com.example.myapplicationktkt2.model.AdvancedUserViewModel

/**
 * 高级用户界面
 *
 * 这些示例展示了以下 LiveData 的关键概念：
 * 基础用法：
 * 使用 MutableLiveData 存储可变数据
 * 使用 LiveData 暴露不可变数据以确保数据安全
 * 在 Activity/Fragment 中观察数据变化
 * 高级用法：
 * 使用 Transformations.map 和 switchMap 创建计算属性
 * 使用 MediatorLiveData 合并多个数据源
 * 结合 ViewModel 和协程处理异步操作
 * 生命周期感知：
 * LiveData 自动管理观察者的生命周期
 * 避免内存泄漏和崩溃
 * 这些示例可以帮助你理解如何在实际项目中使用 LiveData 来构建响应式和安全的 Android 应用程序。
 *
 */
class AdvancedUserActivity : AppCompatActivity() {
    
    private lateinit var viewModel: AdvancedUserViewModel
    
    // UI组件
    private lateinit var firstNameEditText: EditText
    private lateinit var lastNameEditText: EditText
    private lateinit var ageEditText: EditText
    private lateinit var updateButton: Button
    private lateinit var loadButton: Button
    private lateinit var fullNameTextView: TextView
    private lateinit var userTypeTextView: TextView
    private lateinit var userSummaryTextView: TextView
    private lateinit var userDataTextView: TextView
    private lateinit var loadingProgressBar: ProgressBar
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_advanced_user)
        
        initViews()
        setupViewModel()
        observeData()
        setupClickListeners()
    }
    
    private fun initViews() {
        firstNameEditText = findViewById(R.id.et_first_name)
        lastNameEditText = findViewById(R.id.et_last_name)
        ageEditText = findViewById(R.id.et_age)
        updateButton = findViewById(R.id.btn_update)
        loadButton = findViewById(R.id.btn_load)
        fullNameTextView = findViewById(R.id.tv_full_name)
        userTypeTextView = findViewById(R.id.tv_user_type)
        userSummaryTextView = findViewById(R.id.tv_user_summary)
        userDataTextView = findViewById(R.id.tv_user_data)
        loadingProgressBar = findViewById(R.id.progress_bar)
    }
    
    private fun setupViewModel() {
        viewModel = ViewModelProvider(this)[AdvancedUserViewModel::class.java]
    }
    
    private fun observeData() {
        // 观察全名
        viewModel.fullName.observe(this) { fullName ->
            fullNameTextView.text = "全名: $fullName"
        }
        
        // 观察用户类型
        viewModel.userType.observe(this) { userType ->
            userTypeTextView.text = "用户类型: $userType"
        }
        
        // 观察用户摘要
        viewModel.userSummary.observe(this) { summary ->
            userSummaryTextView.text = summary
        }
        
        // 观察加载状态
        viewModel.isLoading.observe(this) { isLoading ->
            if (isLoading) {
                loadingProgressBar.visibility = View.VISIBLE
                userDataTextView.visibility = View.GONE
            } else {
                loadingProgressBar.visibility = View.GONE
                userDataTextView.visibility = View.VISIBLE
            }
        }
        
        // 观察加载的用户数据
        viewModel.userData.observe(this) { userData ->
            userDataTextView.text = userData
        }
    }
    
    private fun setupClickListeners() {
        updateButton.setOnClickListener {
            val firstName = firstNameEditText.text.toString()
            val lastName = lastNameEditText.text.toString()
            val age = ageEditText.text.toString().toIntOrNull() ?: 0
            
            viewModel.updateFirstName(firstName)
            viewModel.updateLastName(lastName)
            viewModel.updateAge(age)
        }
        
        loadButton.setOnClickListener {
            viewModel.loadUserData()
        }
    }
}